#include <func.h>

char*  server_cd(char argc[]){
    char*src=getcwd(NULL,0);
    char*path = argc;
    char*res=src;
    if(!strcmp(path,".")){
        return src;
    }
    else if(!strcmp(path,"..")){
        char*q;
        int m;
        char*p = strtok(src,"/");
        while((p = strtok(NULL,"/"))!=NULL){
            q=p;
        }
        p=strtok(src,"/");
        strcat(res,"/");
        strcat(res,p);
        while((p = strtok(NULL,"/")) !=NULL && (m = strcmp(p,q))!=0){
            strcat(res,"/");
            strcat(res,p);
        }
    }
    else{
        char*p = strtok(path,"/");
        strcat(res,"/");
        strcat(res,p);
        while((p = strtok(NULL,"/")) != NULL){
            strcat(res,"/");
            strcat(res,p);
        }
    }
    if(chdir(res)==-1){
        error(1,errno,"chdir failed");
    }
    return res;
}

